<template>
  <view class="info-page">
    <view class="info-list">
      <view class="info-nav">
        <view class="nav-title">用户ID</view>
        <view class="nav-label">
          <view class="nav-name">{{userInfo.id}}</view>
        </view>
      </view>
      <view class="info-nav">
        <view class="nav-title">头像</view>
        <view class="nav-label">
          <view class="nav-avatar">
            <upload-avatar @UploadAvatar="onUploadAvatar" :src="userInfo.avatar"></upload-avatar>
          </view>
          <view class="nav-icon">
            <up-icon name="arrow-right"></up-icon>
          </view>
        </view>
      </view>
      <view class="info-nav" @click="onShowType(1)">
        <view class="nav-title">昵称</view>
        <view class="nav-label">
          <view class="nav-name">{{userInfo.nickname}}</view>
          <view class="nav-icon">
            <up-icon name="arrow-right"></up-icon>
          </view>
        </view>
      </view>
      <view class="info-nav">
        <view class="nav-title">手机号</view>
        <view class="nav-label">
          <view class="nav-name">{{userInfo.mobile}}</view>
        </view>
      </view>
      <view class="info-nav" @click="toAuthen">
        <view class="nav-title">实名认证</view>
        <view class="nav-label">
          <view class="nav-name">{{userInfo.realname?'已实名':'未实名'}}</view>
        </view>
      </view>
      <view class="info-nav" @click="onShowType(2)">
        <view class="nav-title">注销账号</view>
        <view class="nav-label">
          <view class="nav-name">注销后无法恢复，请谨慎操作</view>
          <view class="nav-icon">
            <up-icon name="arrow-right"></up-icon>
          </view>
        </view>
      </view>
    </view>
    <view class="info-btn">
      <up-button text="退出登录" shape="circle" color="#A4682F" @click="onUnLogin"></up-button>
    </view>
  </view>
  <up-modal :show="nameShow" :title="showType ===1 ?'编辑昵称' :'注销账号'" showCancelButton
    :confirmColor="showType === 1? '#A4682F':'#F52020'" :confirmText="showType ===1 ?'确认' :'确认注销'"
    :cancelText="showType ===1 ?'取消' :'再想想'" @cancel="nameShow = false" @confirm="onConfirmName">
    <view class="slot-content">
      <up-input placeholder="请填写新昵称" border="none" v-model="name" inputAlign="center" v-show="showType ===1"></up-input>
      <view class="content-title" v-show="showType ===2">
        您将注销账号为{{userInfo.mobile}}，账号注销后无法恢复，该账号的资产、权益、记录等一切内容都将无法使用，请谨慎操作
      </view>
    </view>
  </up-modal>
  <up-modal :show="RealShow" title="实名认证" confirmColor="#A4682F" confirmText="好的" @confirm="RealShow = false">
    <view class="slot-content">
      <view class="content-main">真实姓名：{{userInfo.realname}}</view>
      <view class="content-main">身份证号：{{userInfo.idcard}}</view>
    </view>
  </up-modal>

</template>

<script setup>
  import {
    profile,
    writeOff,
    info
  } from './api.js'
  import {
    reactive,
    ref
  } from 'vue'
  import {
    onLoad,
    onShow
  } from '@dcloudio/uni-app'
  const nameShow = ref(false);
  const showType = ref(1)
  const name = ref('')
  const content = ref('')
  const avatar = ref('')
  const userInfo = ref([{}])
  const RealShow = ref(false)
  onLoad(() => {
    userInfo.value = uni.getStorageSync('user')
  })
  onShow(() => {
    userInfo.value = uni.getStorageSync('user')
  })

  function onConfirmName() {
    if (showType.value === 1) {
      profileApi()
    } else {
      writeOffApi()
    }
    nameShow.value = false
  }
  //修改信息
  const profileApi = (async () => {
    await profile({
      nickname: name.value,
      avatar: avatar.value
    })
    await infoApi()
  })
  //账号注销
  const writeOffApi = (async () => {
    const res = await writeOff()
    if (res && res.code == 1) {
      uni.showToast({
        title: "注销成功",
        icon: 'none'
      })
      setTimeout(() => {
        uni.clearStorage()
        uni.switchTab({
          url: '/pages/index/index'
        })
      }, 1000)
    }
  })

  const infoApi = (async () => {
    const res = await info()
    if (res && res.code === 1) {
      uni.showToast({
        title: "修改成功",
        icon: 'none'
      })
      uni.setStorageSync("user", res.data)
      userInfo.value = res.data
    } else {
      uni.showToast({
        title: res.msg,
        icon: "none"
      })
    }
  })

  function onUploadAvatar(val) {
    avatar.value = val
    profileApi()
  }

  function onUnLogin() {
    uni.removeStorageSync('userInfo')
    uni.removeStorageSync('user')
    uni.switchTab({
      url: '/pages/my/my'
    })
  }

  function onShowType(id) {
    showType.value = id
    nameShow.value = true
  }

  function toAuthen() {
    if (!userInfo.value.realname) {
      uni.navigateTo({
        url: '/pages/my/otherFunctionPage/authenticationName'
      })
    } else {
      RealShow.value = true
    }
  }
</script>

<style lang="scss" scoped>
  .info-page {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    padding: 30rpx;

    .info-list {
      background-color: #fff;
      display: flex;
      width: 100%;
      flex-direction: column;
      box-sizing: border-box;
      padding: 0 30rpx;
      border-radius: 16rpx;

      .info-nav {
        width: 100%;
        height: 88rpx;
        border-bottom: 2rpx solid rgba(235, 235, 235, 1);
        display: flex;
        align-items: center;
        justify-content: space-between;

        .nav-title {
          font-size: 28rpx;
          font-weight: 400;
          color: rgba(51, 51, 51, 1);

        }

        .nav-label {
          display: flex;
          align-items: center;

          .nav-avatar {
            width: 48rpx;
            height: 48rpx;
            border-radius: 50%;

          }

          .nav-name {
            font-size: 28rpx;
            font-weight: 400;
            color: rgba(153, 153, 153, 1);

          }

          .nav-icon {
            margin-left: 16rpx;
          }
        }
      }

      .info-nav:last-child {
        border: none;
      }
    }

    .info-btn {
      width: 100%;
      height: 88rpx;
      margin-top: 40rpx;
      display: flex;
      align-items: center;

      ::v-deep .u-button {
        height: 88rpx;
      }
    }
  }

  .slot-content {
    width: 100%;

    ::v-deep .u-input {
      box-sizing: border-box;
      width: 100%;
      background-color: #F7F8FA;
      height: 88rpx;
      border-radius: 16rpx;
      text-align: center;
    }

    .content-main {
      font-size: 30rpx;
      font-weight: 400;
      color: rgba(51, 51, 51, 1);
      text-align: center;
    }
  }
</style>